package com.sprouting.ops.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.interfaces.UpdGroup;
import com.sprouting.ops.model.DromaraFileStorage;
import com.sprouting.ops.model.OpsTool;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.model.qo.OpsToolQo;
import com.sprouting.ops.service.DromaraFileStorageService;
import com.sprouting.ops.service.OpsToolService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 工具;(OpsTool)(OPS_TOOL) 表控制层
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-07-22 14:33:37
 */
@Slf4j
@RestController
@RequestMapping("opsTool")
public class OpsToolController {

    @Resource
    private OpsToolService opsToolService;
    @Resource
    private DromaraFileStorageService dromaraFileStorageService;



    /**
     * 分页查询工具
     * @param query 查询参数
     * @return
     */
    @PostMapping("getListPage")
    public ResultJson getListPage(OpsToolQo query){
        Page page = query.getPage();
        return ResultJson.success(opsToolService.getListPage(page, query));
    }

    /**
     * 删除工具
     * @param id 工具;id
     * @return
     */
    @PostMapping("delOpsTool")
    @IdempotentSingle
    public ResultJson delOpsTool(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorCheck();
        }

        OpsTool tool = opsToolService.getById(id);
        if (tool == null){
            return ResultJson.success();
        }

        DromaraFileStorage opsUploadFile = dromaraFileStorageService.delFileBefore(tool.getIco());

        List<DromaraFileStorage> bindList = dromaraFileStorageService.getByBindId(tool.getId());

        opsToolService.delOpsTool(tool, opsUploadFile, bindList);
        return ResultJson.success();
    }

    /**
     * 更新工具
     * @param bo 修改参数
     * @return
     */
    @PostMapping("updOpsTool")
    @IdempotentSingle
    public ResultJson updOpsTool(@Validated(UpdGroup.class) OpsTool bo){
        String msg = checkOpsTool(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        OpsTool oldDb = opsToolService.getById(bo.getId());
        DromaraFileStorage[] uploadFileArray = dromaraFileStorageService.updFileBefore(bo.getIco(), oldDb.getIco(), bo.getId(), ConstantBase.FileSourceType.TOOL_ICO);

        // 提取Markdown的数据
        // 修改的时候也要提取，然后转换为生产的数据，这里要注意的是，修改的时候，不考虑去删除旧的数据的问题，到时候删除的时候，直接使用objId统一处理即可，因为这个数据量还是比较小的，没必要找的旧的去删，新的去加，搞麻烦了
        List<DromaraFileStorage> installList = dromaraFileStorageService.getMarkdownImagesUrlToFileStorage(bo.getInstallDoc(), bo.getId(), ConstantBase.FileSourceType.TOOL_INSTALL_DOC);

        EntityTool.updBase(bo);
        opsToolService.updOpsTool(bo, uploadFileArray, installList);
        return ResultJson.success(bo);
    }

    /**
     * 保存工具
     * @param bo 保存参数
     * @return
     */
    @PostMapping("saveOpsTool")
    @IdempotentSingle
    public ResultJson saveOpsTool(@Validated(AddGroup.class) OpsTool bo){
        String msg = checkOpsTool(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        bo.setId(IdTool.getId());
        EntityTool.saveBase(bo);

        // 处理文件
        DromaraFileStorage unifiedFileInfo = dromaraFileStorageService.toProductionBefore(bo.getIco(), bo.getId(), ConstantBase.FileSourceType.TOOL_ICO);
        // 提取Markdown的数据
        List<DromaraFileStorage> installList = dromaraFileStorageService.getMarkdownImagesUrlToFileStorage(bo.getInstallDoc(), bo.getId(), ConstantBase.FileSourceType.TOOL_INSTALL_DOC);

        opsToolService.saveOpsTool(bo, unifiedFileInfo, installList);
        return ResultJson.success(bo);
    }


    /**
     * 工具;数据校验
     * @param bo 工具; 校验数据
     * @return 非null则有错误
     */
    private String checkOpsTool(OpsTool bo){

        return null;
    }

}
